<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="searchForm">
        <el-form-item>
          <el-input v-model="searchForm.keywords" placeholder="宠物名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"  v-on:click="inquire">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :disabled="this.sels.length===0" @click="onsale">上架</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :disabled="this.sels.length===0" @click="offsale">下架</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pets" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="名称">
      </el-table-column>
      <el-table-column prop="saleprice" label="售价">
      </el-table-column>
      <el-table-column prop="costprice" label="成本价">
      </el-table-column>
      <el-table-column prop="state" label="状态">
        <template scope="scope">
          <span v-if="scope.row.state==0" style="color: red">下架</span>
          <span v-if="scope.row.state!=0" style="color: green">上架</span>
        </template>
      </el-table-column>
      <el-table-column prop="offsaletime" label="下架时间">
      </el-table-column>
      <el-table-column prop="onsaletime" label="上架时间">
      </el-table-column>
      <el-table-column prop="petType.name" label="品种">
      </el-table-column>
      <el-table-column prop="shop.name" label="店铺">
      </el-table-column>
      <el-table-column prop="user.username" label="用户">
      </el-table-column>

      <el-table-column label="操作" width="200">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes,prev, pager, next"
                     @current-change="handleCurrentChange"
                     @size-change="handleSizeChange"
                     :current-page="currentPage"
                     :page-size="pageSize"
                     :total="total"
                     :page-sizes="[5, 10, 15, 20]"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑 新增 界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">

      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="售价" prop="saleprice">
          <el-input v-model="editForm.saleprice" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="成本价" prop="costprice">
          <el-input v-model="editForm.costprice" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="品种">
          <div class="block">
            <el-cascader v-model="editForm.petType"
                         :options="options"
                         :props="{ checkStrictly: true,
                        value:'id',
                        label:'name'
                        }"
                         clearable></el-cascader>
          </div>
        </el-form-item>

        <el-form-item label="店铺" prop="shop">
          <el-select v-model="editForm.shop" value-key="id" placeholder="请选择">
            <el-option
                v-for="item in shops"
                :key="item.id"
                :label="item.name"
                :value="item">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.address }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="资源" prop="resources">
          <el-input v-model="editForm.resources" auto-complete="off"></el-input>
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs"
              ref="upload"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :file-list="fileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="简介" prop="intro">
          <!--          <el-input v-model="editForm.petDetail.intro" auto-complete="off"></el-input>-->
          <quill-editor v-model="editForm.petDetail.intro" :options="quillConfig">
          </quill-editor>
        </el-form-item>
        <el-form-item label="预约须知" prop="adoptNotice">
          <!--          <el-input v-model="editForm.petDetail.adoptNotice" auto-complete="off"></el-input>-->
          <quill-editor v-model="editForm.petDetail.adoptNotice" :options="quillConfig">
          </quill-editor>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import {quillEditor} from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import quillConfig from '../../common/js/quill-config.js'

export default {
  components: {
    quillEditor
  },
  data() {
    return {
      shops: null,
      options: null,
      quillConfig,
      fileList: [],
      title: '',
      searchForm: {
        keywords: ''
      },
      pets: [],
      total: 0,
      currentPage: 1,
      pageSize: 5,
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      editForm: {
        petType: null,
        id: null,
        name: null,
        costprice: null,
        saleprice: null,
        type_id: null,
        resources: null,
        state: null,
        offsaletime: null,
        onsaletime: null,
        createtime: null,
        shop_id: null,
        user_id: null,
        shop: null,
        user: null,
        petDetail: {
          intro: null,
          adoptNotice: null,
        }
      },

      addFormVisible: false,//新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      }

    }
  },
  methods: {
    getShop(){
      this.$http.get('/shop')
          .then(result => {
            this.shops = result.data
          })
          .catch(result => {
            console.log(result)
            this.$message({
              message: '查询店铺，系统错误',
              type: 'error'
            });
          })
    },
    getPetTypeTerr() {
      this.$http.get("/petType/cascade")
          .then(result => {
            this.options = result.data;
          })
          .catch(result => {
            this.$message({
              message: '系统错误，请稍后再试',
              type: 'error'
            });
          })
    },
    onsale() {
      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if (!this.sels || this.sels.length < 1) {
        this.$message({message: '老铁，你不选中数据，臣妾上架不了啊....', type: 'error'});
        return;
      }
      this.$confirm('确认上架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/pet/onsale', ids).then((res) => {
          this.listLoading = false;

          if (res.data.success) {
            this.$message({
              message: '上架成功',
              type: 'success'
            });
          } else {
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
          this.getPets();
        });
      }).catch(() => {

      });
    },
    offsale() {

      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if (!this.sels || this.sels.length < 1) {
        this.$message({message: '老铁，你不选中数据，臣妾下架不了啊....', type: 'error'});
        return;
      }
      this.$confirm('确认下架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/pet/offsale', ids).then((res) => {
          this.listLoading = false;

          if (res.data.success) {
            this.$message({
              message: '下架成功',
              type: 'success'
            });
          } else {
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
          this.getPets();
        });
      }).catch(() => {

      });
    },
    handleSuccess(response, file, fileList) {
      //resultObj 文件的服务器地址，放到数据库中
      //根据双向绑定 logo 就有值 1,2,3
      let resourcesTmp = '';
      if (fileList != null && fileList.length > 0) {
        for (var i = 0; i < fileList.length; i++) {
          if (i == 0) {
            resourcesTmp = resourcesTmp + fileList[i].response.resultObj;
          } else {
            resourcesTmp = resourcesTmp + "," + fileList[i].response.resultObj;
          }
        }
      }
      this.editForm.resources = resourcesTmp;
    },
    handleRemove(file, fileList) {
      var filePath = file.response.resultObj;
      this.$http.delete("/fastDfs/?path=" + filePath)
          .then(res => {
            if (res.data.success) {
              this.$message({
                message: '删除成功!',
                type: 'success'
              });
            } else {
              this.$message({
                message: '删除失败!',
                type: 'error'
              });
            }
          })

      //删除图片后移除data里面数据
      let resourcesTmp = '';
      if (fileList != null && fileList.length > 0) {
        for (var i = 0; i < fileList.length; i++) {
          if (i == 0) {
            resourcesTmp = resourcesTmp + fileList[i].response.resultObj;
          } else {
            resourcesTmp = resourcesTmp + "," + fileList[i].response.resultObj;
          }
        }
      }
      this.editForm.resources = resourcesTmp;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getPets();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getPets();
    },
    inquire(){
      this.currentPage = 1;
      this.getPets();
    },

    //获取用户列表
    getPets() {
      let para = {
        keywords: this.searchForm.keywords,

        currentPage: this.currentPage,

        pageSize: this.pageSize,

      };
      //打开加载的效果
      this.listLoading = true;
      //
      this.$http.post("/pet/list", para).then(res => {
        this.pets = res.data.rows;
        this.total = res.data.total;
        //关闭加载的效果
        this.listLoading = false;
      });
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        //开启加载的模态框
        this.listLoading = true;
        //NProgress.start();
        //后台地址的请求
        this.$http.delete("/pet/" + row.id).then((res) => {
          this.listLoading = false;
          if (res.data.success) {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }

          this.getPets();
        });
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      //回显
      this.editForm = Object.assign({}, row);
      // 将宠物类型的级联路径以"/"分割成一个数组
      let arr = this.editForm.petType.dirPath.split("/");
      // 创建一个数组，用来接收参数
      let echoArr = [];
      //  遍历数组
      for (var i = 1; i < arr.length; i++) {
        echoArr.push(parseInt(arr[i]))
      }
      // 将数组赋值给宠物类型
      this.editForm.petType = echoArr
      // 回显详情
      console.log("=========row打印===========")
      console.log(row.id)
      this.$http.get("/petDetail/petId/" + row.id)
          .then(result => {
            this.editForm.petDetail = result.data
            console.log("详情回显")
            console.log(this.editForm.petDetail)
          }).catch(result => {
        console.log(result)
        this.$message({
          message: "回显系统错误",
          type: 'error'
        });
      });
      //图片回显
      this.fileList = [];
      var resources = row.resources;
      var resourcesArr = resources.split(",");
      for (var i = 0; i < resourcesArr.length; i++) {
        this.fileList.push({url: "http://123.207.27.208" + resourcesArr[i]})
      }
    },
    //显示新增界面
    handleAdd: function () {
      //打开弹出框
      this.editFormVisible = true;
      this.title = '新增宠物';
      this.$refs.upload.clearFiles();
      //初始化表单字段-利用的双向绑定的特性
      this.editForm = {
        // 品种
        petType: null,
        id: null,
        // 名称
        name: null,
        // 成本价
        costprice: null,
        // 售价
        saleprice: null,
        // log图片
        resources: null,
        // 店铺
        shop: null,
        // 购买用户
        user: null,

        petDetail: {
          // 介绍
          intro: null,
          // 购买通知
          adoptNotice: null,
        }
      };
    },
    //编辑
    editSubmit: function () {
      //验证当前的表单是否符合验证规则
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            //把表单的数据组装为json
            let para = Object.assign({}, this.editForm);
            if (para.petType) {
              para.petType = {"id": para.petType[para.petType.length - 1]}
            }
            console.log("================")
            console.log(para)
            //para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
            this.$http.put('/pet', para).then(restlu => {
              this.editLoading = false;
              //NProgress.done();
              restlu = restlu.data
              if (restlu.success) {
                this.$message({
                  message: "成功",
                  type: 'success'
                });
              } else {
                this.$message({
                  message: "参数或者其他出现故障",
                  type: 'error'
                });
              }

              //重置表单-调用现成的方法
              this.$refs['editForm'].resetFields();
              //关闭 加载框
              this.editFormVisible = false;
              //重新刷新页面数据
              this.getPets();
            }).catch(restlu => {
              console.log(restlu)
              this.$message({
                message: "系统错误",
                type: 'error'
              });
            })
            ;
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = {ids: ids};
        this.$http.patch('/pet', ids).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getPets();
        });
      }).catch(() => {

      });
    }
  },

  mounted() {
    //当页面加载完以后。执行的方法
    this.getPets();
    this.getPetTypeTerr();
    this.getShop();
  }
}

</script>

<style scoped>

</style>